package com.koron.car.web.motorcade.service.impl;

import com.koron.car.web.motorcade.bean.PrimaryOrg;
import com.koron.car.web.motorcade.mapper.PrimaryOrgMapper;
import com.koron.car.web.motorcade.service.PrimaryOrgService;
import com.koron.common.core.util.CodeTools;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * (PrimaryOrg)表服务实现类
 *
 * @author twcao
 * @since 2022-03-15 11:35:41
 */
@Service
public class PrimaryOrgServiceImpl implements PrimaryOrgService {

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    @TaskAnnotation("queryById")
    public PrimaryOrg queryById(SessionFactory factory, String id) {
        PrimaryOrgMapper mapper = factory.getMapper(PrimaryOrgMapper.class);
        return mapper.queryById(id);
    }

    /**
     * 查询多条数据
     * @param primaryOrg 查询条件
     * @return 对象列表
     */
    @Override
    @TaskAnnotation("queryList")
    public List<PrimaryOrg> queryList(SessionFactory factory, PrimaryOrg primaryOrg) {
        PrimaryOrgMapper mapper = factory.getMapper(PrimaryOrgMapper.class);
        return mapper.queryList(primaryOrg);
    }

    /**
     * 新增数据
     *
     * @param primaryOrg 实例对象
     * @return 实例对象
     */
    @Override
    @TaskAnnotation("insert")
    public PrimaryOrg insert(SessionFactory factory, PrimaryOrg primaryOrg) {
        PrimaryOrgMapper mapper = factory.getMapper(PrimaryOrgMapper.class);
        primaryOrg.setId(CodeTools.getCode32());
        mapper.insert(primaryOrg);
        return primaryOrg;
    }

    @Override
    @TaskAnnotation("insertOrUpdateBatch")
    public int insertOrUpdateBatch(SessionFactory factory, List<PrimaryOrg> primaryOrgs) {
        PrimaryOrgMapper mapper = factory.getMapper(PrimaryOrgMapper.class);
        mapper.delete();
        primaryOrgs.stream().forEach(row -> row.setId(StringUtils.isBlank(row.getId()) ? CodeTools.getCode32() : row.getId()));
        mapper.insertOrUpdateBatch(primaryOrgs);
        return primaryOrgs.size();
    }

    /**
     * 是否是基层
     *
     * @param factory
     * @param orgIds
     * @return 返回是基层的组织id
     */
    @Override
    @TaskAnnotation("isPrimary")
    public List<String> isPrimary(SessionFactory factory, List<String> orgIds) {
        PrimaryOrgMapper mapper = factory.getMapper(PrimaryOrgMapper.class);
        List<String> primary = mapper.isPrimary(orgIds);
        return primary;
    }

    /**
     * 修改数据
     *
     * @param primaryOrg 实例对象
     * @return 实例对象
     */
    @Override
    @TaskAnnotation("update")
    public PrimaryOrg update(SessionFactory factory, PrimaryOrg primaryOrg) {
        PrimaryOrgMapper mapper = factory.getMapper(PrimaryOrgMapper.class);
        mapper.update(primaryOrg);
        return queryById(factory, primaryOrg.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    @TaskAnnotation("deleteById")
    public boolean deleteById(SessionFactory factory, String id) {
        PrimaryOrgMapper mapper = factory.getMapper(PrimaryOrgMapper.class);
        return mapper.deleteById(id) > 0;
    }
}
